a = int(input())
s = []
s.append(a)
a = str(a)
b = []
if 1000 > int(a) or int(a) > 9999:
    print(0)
    exit()
for i in range(4): 
    s.append(int(a[i]))
    if i != 3:
        s.append(int(a[i:i+2]))
    if i != 2:
        s.append(int(a[i:i + 3]))
for i in s:
    for j in range(2, i):
        if i % j == 0:
            break
    else:
        b.append(i)
if b == []:
    print(0)
    exit()
else:
    print(max(b))